package com.thd.springboottest.redis.utils;

import com.thd.springboottest.redis.cfg.JedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * JedisUtil(推荐存Byte数组，存Json字符串效率更慢)
 */
@Component
@ConditionalOnClass(value={JedisConfig.class})
public class JedisUtil {

    private static String OK = "OK";
    /**
     * 静态注入JedisPool连接池
     * 本来是正常注入JedisUtil，可以在Controller和Service层使用，但是重写Shiro的CustomCache无法注入JedisUtil
     * 现在改为静态注入JedisPool连接池，JedisUtil直接调用静态方法即可
     */

    private static JedisPool jedisPool;

    public static JedisPool getJedisPool() {
        return jedisPool;
    }

    @Autowired
    public void setJedisPool(JedisPool jedisPool) {
        JedisUtil.jedisPool = jedisPool;
    }


    /**
     * 获取redis键值-object
     *
     * @param key
     * @return java.lang.Object
     */
    public static Object getObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            if (!StringUtils.isEmpty(bytes)) {
                return MySerializableUtil.unserializable(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
        return null;
    }

    /**
     * 设置redis键值-object
     *
     * @param key
     * @param value
     * @return java.lang.String
     */
    public static String setObject(String key, Object value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(key.getBytes(), MySerializableUtil.serializable(value));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * 设置redis键值-object-expiretime
     *
     * @param key
     * @param value
     * @param expiretime
     * @date 2018/9/4 15:50
     */
    public static String setObject(String key, Object value, int expiretime) {
        String result;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.set(key.getBytes(), MySerializableUtil.serializable(value));
            if (JedisUtil.OK.equals(result)) {
                jedis.expire(key.getBytes(), expiretime);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * 获取redis键值-Json
     *
     * @param key
     * @return java.lang.Object
     */
    public static String getJson(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * 设置redis键值-Json
     *
     * @param key
     * @param value
     * @return java.lang.String
     */
    public static String setJson(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * 设置redis键值-Json-expiretime
     *
     * @param key
     * @param value
     * @param expiretime
     * @return java.lang.String
     */
    public static String setJson(String key, String value, int expiretime) {
        String result;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.set(key, value);
            if (JedisUtil.OK.equals(result)) {
                jedis.expire(key, expiretime);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * 删除key
     *
     * @param key
     * @return java.lang.Long
     */
    public static Long delKey(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }

    /**
     * key是否存在
     *
     * @param key
     * @return java.lang.Boolean
     */
    public static Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }
        }
    }

    /**
     * 获取过期剩余时间
     *
     * @param key
     * @return java.lang.String
     */
    public static Long ttl(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.ttl(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage());
        }finally {
            if(null != jedis) {
                jedis.close();
            }

        }
    }
}
